dojo.require("dojo.widget.TabContainer");

var container = document.getElementById(widget.uuid);

var tabbedview = dojo.widget.createWidget("TabContainer", null, container);
var tabs = [];
// pull in the arguments
if (typeof widget.value != "undefined") {
  
  // convert the tabs object into an array
  if (typeof widget.value.tabs == "object") {
      for (var i in widget.value.tabs) {
        var row = [];
        for (var ir in widget.value.tabs[i] ) {  
            row[ir] = widget.value.tabs[i][ir];
        }
        tabs.push(row);
      }
  }
} else {
    // default some data;
    tabs = [
                {label: 'Tab 0', content: 'This is the default data provided for a tabbed view in the component.js file.' +
                ' To customize the tabs set the value of the widget to an object containing an object with a tabs property containg' +
                ' an array of tabs. For example: {tabs:[{label:\'My Tab\', content: \'Some Content\'},{label:\'My Tab 2\', content: \'Tab 2 Content\'} ]}' +
                '. You can also include content from any URL within your domain. Example: {tabs:[{label:\'My Tab\', url: \'URL_TO_TAB_CONTENT\'} ]}'},
                {label: 'Tab 1', content: 'Tab 1 content'},
                {label: 'Tab 2', content: 'Tab 2 content'}
            ];    
}


for(i=0; i<tabs.length; ++i) {
    var _row = tabs[i];
    if (typeof _row.url == 'undefined') {
        var content = dojo.widget.createWidget("ContentPane", {label: tabs[i].label, selected: i==1});
        content.setContent(_row.content);
        tabbedview.addChild(content);
    } else {
        var callback = function(xhr) {
            if (xhr.readyState == 4) {
            var _c = dojo.widget.createWidget("ContentPane", {label: _row.label, selected: i==1});
                    _c.setContent(xhr.responseText);
                    tabbedview.addChild(_c);
                    
            }
        }
        // go out and fetch the url synchronously

        var xhr = jmaki.doAjax({
            method:"GET",
            url: _row.url,
            asynchronous: false,
            callback: callback
           }
           );
    }
    
}
tabbedview.onResized();
jmaki.attributes.put(widget.uuid, tabbedview);